#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include <sstream>
#include <string>
#include <iostream>


int main(int argc, char** argv)
{
	char imageFile1[100],imageFile2[100];
	char dirPicOri[100];
	int count=1;
	double time1;
		
	CvCapture* capture;
	IplImage* frame;
	IplImage* frame2;

	double time2 = (double)cvGetTickCount();	
	
	cvNamedWindow( "Video", CV_WINDOW_AUTOSIZE );
	
	//////Changed parameters
	capture = cvCreateFileCapture( "webcam20100503/outA.avi" );  //video file
	sprintf(dirPicOri, "webcam20100503/outA");  //folder to keep captured file
	int startFrame = 1;  //start frame to capture
	int startWithZero = 0;

	//capture = cvCreateFileCapture( argv[1] );
	while( true ) {
	     		
		frame = cvQueryFrame( capture );
		
		if( !frame ) break;
		cvShowImage( "Video", frame );	
				
		if(count>startFrame){
			frame2 = cvCreateImage(cvSize(frame->width,frame->height),8,1);
			cvCvtColor(frame,frame2,CV_BGR2GRAY);
			if(startWithZero){
				if(count<10){ 
					sprintf(imageFile1,"%s/pic_000%d.png",dirPicOri,count); 
				}else if(count<100){ 
					sprintf(imageFile1,"%s/pic_00%d.png",dirPicOri,count); 				
				}else if(count<1000){ 
					sprintf(imageFile1,"%s/pic_0%d.png",dirPicOri,count); 				
				}else { 			
					sprintf(imageFile1,"%s/pic_%d.png",dirPicOri,count); 				
				}
			}else{
				sprintf(imageFile1,"%s/pic_%d.png",dirPicOri,count);
			}
			cvSaveImage(imageFile1,frame);
		}		
	    
	    count=count+1;
	    
		char c = cvWaitKey(33);
		if( c == 27 ) break;
	}
	cvReleaseCapture( &capture );
	cvDestroyWindow( "Video" );

    return 0;
}
